// Pipeline Component Library Example
// ----------------------------------
// Colin Bowern (colinbowern@hotmail.com)
// January 2007
//
// Copyright (c) 2007 Colin Bowern
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// * The above copyright notice and this permission notice shall be included in
//   all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#region References
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Globalization;
using System.Runtime.InteropServices;
#endregion

namespace RockstarGuys.PipelineComponents
{
    /// <summary>
    /// Registers the pipeline components for use in the Pipeline Editor.
    /// </summary>
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="Installer"/> class.
        /// </summary>
        public Installer()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods
        /// <summary>
        /// When overridden in a derived class, performs the installation.
        /// </summary>
        /// <param name="stateSaver">An <see cref="T:System.Collections.IDictionary"></see> used to save information needed to perform a commit, rollback, or uninstall operation.</param>
        /// <exception cref="T:System.ArgumentException">The stateSaver parameter is null. </exception>
        /// <exception cref="T:System.Exception">An exception occurred in the <see cref="E:System.Configuration.Install.Installer.BeforeInstall"></see> event handler of one of the installers in the collection.-or- An exception occurred in the <see cref="E:System.Configuration.Install.Installer.AfterInstall"></see> event handler of one of the installers in the collection. </exception>
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

            Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();

            foreach (Type type in types)
            {
                object[] comVisibleAttributes = type.GetCustomAttributes(typeof(ComVisibleAttribute), true);
                if (comVisibleAttributes.Length > 0)
                {
                    ComVisibleAttribute comVisibleAttribute = (ComVisibleAttribute)comVisibleAttributes[0];
                    if (comVisibleAttribute.Value)
                    {
                        object[] guidAttributes = type.GetCustomAttributes(typeof(GuidAttribute), false);
                        if (guidAttributes.Length > 0)
                        {
                            GuidAttribute guidAttribute = (GuidAttribute)guidAttributes[0];

                            object[] pipelineCategoryAttributes = type.GetCustomAttributes(typeof(PipelineCategoryAttribute), false);
                            foreach (PipelineCategoryAttribute pipelineCategoryAttribute in pipelineCategoryAttributes)
                            {
                                foreach (Guid pipelineCategory in pipelineCategoryAttribute.Categories)
                                {
                                    string registrySubKey = String.Format(CultureInfo.InvariantCulture, @"HKEY_CLASSES_ROOT\CLSID\{0}\Implemented Categories\{1}", new Guid(guidAttribute.Value).ToString("B").ToUpper(CultureInfo.InvariantCulture), pipelineCategory.ToString("B").ToUpper(CultureInfo.InvariantCulture));
                                    Registry.SetValue(registrySubKey, null, string.Empty, RegistryValueKind.String);
                                }
                            }

                            object[] progIdAttributes = type.GetCustomAttributes(typeof(ProgIdAttribute), false);
                            if (progIdAttributes.Length > 0)
                            {
                                ProgIdAttribute progIdAttribute = (ProgIdAttribute)progIdAttributes[0];

                                string registrySubKey = String.Format(CultureInfo.InvariantCulture, @"HKEY_CLASSES_ROOT\CLSID\{0}", new Guid(guidAttribute.Value).ToString("B").ToUpper(CultureInfo.InvariantCulture));
                                Registry.SetValue(registrySubKey, null, progIdAttribute.Value, RegistryValueKind.String);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// When overridden in a derived class, removes an installation.
        /// </summary>
        /// <param name="savedState">An <see cref="T:System.Collections.IDictionary"></see> that contains the state of the computer after the installation was complete.</param>
        /// <exception cref="T:System.ArgumentException">The saved-state <see cref="T:System.Collections.IDictionary"></see> might have been corrupted. </exception>
        /// <exception cref="T:System.Configuration.Install.InstallException">An exception occurred while uninstalling. This exception is ignored and the uninstall continues. However, the application might not be fully uninstalled after the uninstallation completes. </exception>
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);

            Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();

            foreach (Type type in types)
            {
                object[] comVisibleAttributes = type.GetCustomAttributes(typeof(ComVisibleAttribute), true);
                if (comVisibleAttributes.Length > 0)
                {
                    ComVisibleAttribute comVisibleAttribute = (ComVisibleAttribute)comVisibleAttributes[0];
                    if (comVisibleAttribute.Value)
                    {
                        object[] guidAttributes = type.GetCustomAttributes(typeof(GuidAttribute), false);
                        if (guidAttributes.Length > 0)
                        {
                            GuidAttribute guidAttribute = (GuidAttribute)guidAttributes[0];

                            object[] pipelineCategoryAttributes = type.GetCustomAttributes(typeof(PipelineCategoryAttribute), false);
                            foreach (PipelineCategoryAttribute pipelineCategoryAttribute in pipelineCategoryAttributes)
                            {
                                foreach (Guid pipelineCategory in pipelineCategoryAttribute.Categories)
                                {
                                    string registrySubKey = String.Format(CultureInfo.InvariantCulture, @"CLSID\{0}\Implemented Categories\{1}", new Guid(guidAttribute.Value).ToString("B").ToUpper(CultureInfo.InvariantCulture), pipelineCategory.ToString("B").ToUpper(CultureInfo.InvariantCulture));
                                    Registry.ClassesRoot.DeleteSubKey(registrySubKey);
                                }
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}